﻿using System;
using System.Runtime.Serialization.Json;
using System.Windows;
using Microsoft.Phone.Controls;
using WeiboSdk;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Collections.Generic;

namespace MyDeeWeibo
{
    public partial class WeiboContent : PhoneApplicationPage
    {
        #region ShowCommentsListProperty
        public static readonly DependencyProperty ShowCommentsListProperty =
            DependencyProperty.Register(
            "ShowCommentsList",
            typeof(ObservableCollection<WStatus>),
            typeof(WeiboContent),
            new PropertyMetadata((ObservableCollection<WStatus>)null));

        public ObservableCollection<WStatus> ShowCommentsList
        {
            get { return (ObservableCollection<WStatus>)GetValue(ShowCommentsListProperty); }
            set { SetValue(ShowCommentsListProperty, value); }
        }
        #endregion
        
        public WStatus StatusResults;

        private SdkCmdBase cmdBaseCommentsShow;
        private SdkNetEngine netEngineCommentsShow;

        public ObservableCollection<WStatus> DataCollection { get; set; }

        public ObservableCollection<WStatus> BuildCollection()
        {
            DataCollection = new ObservableCollection<WStatus>();
            DataCollection.Add(StatusResults);
            return DataCollection;
        }

        public WeiboContent()
        {
            InitializeComponent();
        }

        public void RefreshCommentsShow()
        {

            // Define a new net engine
            netEngineCommentsShow = new SdkNetEngine();

            // Define a new command base
            cmdBaseCommentsShow = new cmdNormalMessages
            {
                acessToken = App.AccessToken,
                //acessTokenSecret = App.AccessTokenSecret,
                Id = StatusResults.id
            };


            // Request server, the last parameter is set as default (".json")
            netEngineCommentsShow.RequestCmd(SdkRequestType.COMMENTS_SHOW, cmdBaseCommentsShow,
                // Requeset callback
                delegate(SdkRequestType requestType, SdkResponse response)
                {
                    //Deployment.Current.Dispatcher.BeginInvoke(() => ProgressIndicatorIsVisible = false);
                    if (response.errCode == SdkErrCode.SUCCESS)
                    {
                        WStatuses showcomments = null;
                        try
                        {
                            DataContractJsonSerializer scser = new DataContractJsonSerializer(typeof(WStatuses));
                            showcomments = scser.ReadObject(response.stream) as WStatuses;
                        }
                        catch (Exception)
                        {
                            throw;
                        }

                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            ShowCommentsList = new ObservableCollection<WStatus>(showcomments.comments);
                        });
                    }
                    else
                    {
                        //Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show(response.content, response.errCode.ToString(), MessageBoxButton.OK));
                        MessageBox.Show("未授权，请再次点击【登录/授权】按钮进行应用授权！");
                        //Debug.WriteLine(response.content, response.errCode.ToString());
                    }
                });
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            BuildCollection();
            //RefreshCommentsShow();
        }

        private void refreshIconButton_Click(object sender, EventArgs e)
        {
            RefreshCommentsShow();
        }

        private void repostButton_Click(object sender, EventArgs e)
        {

        }

        private void favButton_Click(object sender, EventArgs e)
        {

        }

        private void commentButton_Click(object sender, EventArgs e)
        {

        }
    }
}
